package com.proj.hot.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.proj.hot.GlobalParams;
import com.proj.hot.R;
import com.proj.hot.activity.CustomerServicesActivity;
import com.proj.hot.activity.LoginActivity;
import com.proj.hot.activity.OrderRecordActivity;
import com.proj.hot.activity.SeeRecordActivity;
import com.proj.hot.activity.UserInfoActivity;
import com.proj.hot.utils.DebugUtil;
import com.proj.hot.utils.ImageLoaderConfigFactory;
import com.proj.hot.widget.CircleImageView;

/**
 * 用户中心
 */
public class UserCenterFragment extends BaseFragment {

    private final String TAG = "UserCenterFragment";

    private View userAccount;
    private View orderRecord;
    private View seeRecord;
    private View setting;
    private View customerService;

    private ImageView focus1;
    private ImageView focus2;
    private ImageView focus3;
    private ImageView focus4;
    private ImageView focus5;

    private String actionSetting = android.provider.Settings.ACTION_SETTINGS;


    private CircleImageView user_logo;
    private TextView user_text_name;
    private Button user_btn_cancel;
    private Button user_btn_info;

    private RelativeLayout user_rl_view;



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_usercenter1, container, false);
        userAccount = rootView.findViewById(R.id.user_rl_poster1);
        orderRecord = rootView.findViewById(R.id.user_rl_poster2);
        seeRecord = rootView.findViewById(R.id.user_rl_poster3);
        setting = rootView.findViewById(R.id.user_rl_poster4);
        customerService = rootView.findViewById(R.id.user_rl_poster5);

        user_logo = (CircleImageView) rootView.findViewById(R.id.user_logo);
        user_text_name = (TextView) rootView.findViewById(R.id.user_text_name);
        user_btn_cancel = (Button) rootView.findViewById(R.id.user_btn_cancel);
        user_btn_info = (Button) rootView.findViewById(R.id.user_btn_info);
        user_rl_view = (RelativeLayout) rootView.findViewById(R.id.user_rl_view);

        focus1 = (ImageView) rootView.findViewById(R.id.focus1);
        focus2 = (ImageView) rootView.findViewById(R.id.focus2);
        focus3 = (ImageView) rootView.findViewById(R.id.focus3);
        focus4 = (ImageView) rootView.findViewById(R.id.focus4);
        focus5 = (ImageView) rootView.findViewById(R.id.focus5);

//        if (GlobalParams.screenHeight == 720) {
//            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) user_btn_cancel
//                    .getLayoutParams();
//            lp.setMargins(0, 30, 0, 0);
//            user_btn_cancel.setLayoutParams(lp);
//        }

        if (GlobalParams.screenHeight == 1080) {
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) user_btn_cancel
                    .getLayoutParams();
            lp.height = 60;
            lp.setMargins(0, 50, 0, 0);
            user_btn_cancel.setTextSize(25);
            user_btn_cancel.setLayoutParams(lp);

            RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams) user_btn_info
                    .getLayoutParams();
            lp1.height = 60;
            user_btn_info.setTextSize(25);
            user_btn_info.setLayoutParams(lp1);
        }


        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onResume() {
        super.onResume();
        //如果已经登录
        if (GlobalParams.IS_LOGIN) {
            user_btn_cancel.setText("退出登录");
            ImageLoader.getInstance().displayImage(GlobalParams.user_logo,
                    user_logo, ImageLoaderConfigFactory.getOptionsWithNopic());
            user_text_name.setText(GlobalParams.user_name);
        } else {//未登录
            user_btn_cancel.setText("登录");
            user_logo.setBackgroundResource(R.drawable.user_ic_logon);
            user_text_name.setText("");
        }
        setOnListener();
    }

    private void setOnListener() {
//        userAccount.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent(getActivity().getApplicationContext(), UserInfoActivity.class);
//                startActivity(intent);
//            }
//        });
        user_btn_cancel.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                // user_rl_view.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
                // user_rl_view.setFocusable(true);
            }
        });

        //已登录、按钮变为退出登录，点击清空所有用户数据
        user_btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!GlobalParams.IS_LOGIN) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    GlobalParams.user_paytype = "";
                    GlobalParams.user_id = "";
                    GlobalParams.user_uniqkey = "";
                    GlobalParams.user_phone = "";
                    GlobalParams.user_creater = "";
                    GlobalParams.user_type = "";
                    GlobalParams.user_logo = "";
                    GlobalParams.user_address = "";
                    GlobalParams.user_createTime = "";
                    GlobalParams.user_updater = "";
                    GlobalParams.user_name = "";
                    GlobalParams.user_updateTime = "";
                    user_btn_cancel.setText("登录");
                    user_logo.setImageResource(R.drawable.user_ic_logon);
                    user_text_name.setText("");
                    GlobalParams.IS_LOGIN = false;

                }
            }
        });
        user_btn_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!GlobalParams.IS_LOGIN) {
                    DebugUtil.toast(getActivity().getApplicationContext(), "请先登录!");
                } else {
                    Intent intent = new Intent(getActivity().getApplicationContext(), UserInfoActivity.class);
                    String accountid = GlobalParams.user_id;
                    if (!"".equals(accountid) && null != accountid) {
                        intent.putExtra("accountid", accountid);
                    }
                    startActivity(intent);
                }
            }
        });

        orderRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (GlobalParams.IS_LOGIN) {//如果已经登录，则进入
                    Intent intent = new Intent(getActivity().getApplicationContext(), OrderRecordActivity.class);
//                    String accountid = GlobalParams.user_id;
//                      ;
//                    if (!"".equals(accountid) && null != accountid) {
//                        intent.putExtra("accountid", accountid);
//                    }
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(getActivity().getApplicationContext(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        seeRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (GlobalParams.IS_LOGIN) {//如果已经登录，则进入
                    Intent intent = new Intent(getActivity().getApplicationContext(), SeeRecordActivity.class);
                    String accountid = GlobalParams.user_id;
                    if (!"".equals(accountid) && null != accountid) {
                        intent.putExtra("accountid", accountid);
                    }
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(getActivity().getApplicationContext(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(actionSetting);
//                startActivity(intent);
                showPassWordDialog();
            }
        });
        customerService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity().getApplicationContext(), CustomerServicesActivity.class);
                startActivity(intent);
            }
        });

        user_btn_info.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b){
                    focus1.setImageResource(R.color.transparent);
                   // user_btn_info.setTextColor(getResources().getColor(R.color.lanse));
                }else{
                    focus1.setImageResource(R.color.transparent);
                }
            }
        });

        user_btn_cancel.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b){
                    focus1.setImageResource(R.color.transparent);
                }else{
                    focus1.setImageResource(R.color.transparent);
                }
            }
        });

        orderRecord.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b){
                    focus2.setImageResource(R.color.transparent);
                }else{
                    focus2.setImageResource(R.color.transparent);
                }
            }
        });
        seeRecord.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b){
                    focus3.setImageResource(R.color.transparent);
                }else{
                    focus3.setImageResource(R.color.transparent);
                }
            }
        });
        setting.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b){
                    focus4.setImageResource(R.color.transparent);
                }else{
                    focus4.setImageResource(R.color.transparent);
                }
            }
        });
        customerService.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b){
                    focus5.setImageResource(R.color.transparent);
                }else{
                    focus5.setImageResource(R.color.transparent);
                }
            }
        });

    }



    private void showPassWordDialog() {

        final EditText et = new EditText(getActivity());
        et.setSingleLine();
        new AlertDialog.Builder(getActivity()).
                setTitle("输入密码").
                setIcon(android.R.drawable.ic_dialog_info).
                setView(et).
                setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String input = et.getText().toString().trim();
                        if (input.equals("")) {
                            DebugUtil.toast(getActivity().getApplicationContext(), "密码不能为空!");
                            return;
                        }
                        if (input.equals(GlobalParams.SYSTEM_SETTING_PASSWORD)) {
                            Intent intent = new Intent(actionSetting);
                            startActivity(intent);
                        } else {
                            DebugUtil.toast(getActivity().getApplicationContext(), "密码错误!");
                        }
                    }
                })
                .setNegativeButton("取消", null).show();
    }

}
